package com.quectel.core.module.securitypatrol.service.impl;


import com.quectel.constant.global.SystemConstants;
import com.quectel.core.constants.RedisCacheConstants;
import com.quectel.core.module.securitypatrol.dao.SecurityPatrolPlanDao;
import com.quectel.core.module.securitypatrol.dto.SecurityPatrolPlanDto;
import com.quectel.core.module.securitypatrol.dto.SecurityPatrolRouteDto;
import com.quectel.core.module.securitypatrol.entity.SecurityPatrolPlanEntity;
import com.quectel.core.module.securitypatrol.service.SecurityPatrolPlanService;
import com.quectel.core.module.securitypatrol.service.SecurityPatrolRouteService;
import com.quectel.core.module.village.service.VillageService;
import com.quectel.util.common.CopyUtils;
import com.quectel.util.kit.CacheKit;
import com.quectel.util.kit.Snowflake;
import com.quectel.util.mybatis.MyBatisPlusUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.function.Function;

/**
 * @author caolu
 * @email louis.cao@quectel.com
 * @date 2022/05/07 14:11
 */
@DubboService
public class SecurityPatrolPlanServiceImpl implements SecurityPatrolPlanService {

    private static final Logger LOGGER = LoggerFactory.getLogger(SecurityPatrolPlanServiceImpl.class);

    /**
     * 入参解释:实体id
     */
    private static final Function<Long, String> SECURITY_PATROL_PLAN_CACHE_BY_ID_FUNC = id -> String.format(RedisCacheConstants.PROJECT_NAME + "SECURITY_PATROL_PLAN_CACHE_BY_ID:%d", id);

    @Autowired
    private SecurityPatrolPlanDao securityPatrolPlanDao;
    @Autowired
    private VillageService villageService;
    @Autowired
    private SecurityPatrolRouteService securityPatrolRouteService;


    @Override
    public SecurityPatrolPlanDto selectCacheById(Long id) {
        return CacheKit.cacheToRedis(
                () -> selectById(id),
                SECURITY_PATROL_PLAN_CACHE_BY_ID_FUNC.apply(id),

                SystemConstants.NOT_NULL_CACHE_EXPIRE_SECONDS
        );
    }

    @Override
    public SecurityPatrolPlanDto selectById(Long id) {
        SecurityPatrolPlanEntity entity = securityPatrolPlanDao.selectById(id);
        return paddingField(CopyUtils.copyObj(entity, SecurityPatrolPlanDto.class));
    }

    @Override
    public List<SecurityPatrolPlanDto> queryList(Map<String, Object> params) {
        List<SecurityPatrolPlanEntity> list = securityPatrolPlanDao.queryList(params);
        List<SecurityPatrolPlanDto> result = CopyUtils.copyList(list, SecurityPatrolPlanDto.class);
        result.forEach(this::paddingField);
        return result;
    }

    private SecurityPatrolPlanDto paddingField(SecurityPatrolPlanDto dto) {
        if (dto != null) {
            if (dto.getRouteId() != null) {
                SecurityPatrolRouteDto securityPatrolRouteDto = securityPatrolRouteService.selectById(dto.getRouteId());
                dto.setSecurityPatrolRouteDto(securityPatrolRouteDto);
                dto.setSecurityPatrolRouteName(dto.getRouteName());
                dto.setVillageId(dto.getVillageId());
                dto.setVillageName(villageService.selectCacheById(dto.getVillageId()).getName());
            }

        }
        return dto;
    }

    @Override
    public int queryTotal(Map<String, Object> params) {
        return securityPatrolPlanDao.queryTotal(params);
    }

    @Override
    public Long save(SecurityPatrolPlanDto dto) {
        if (dto.getRouteId() != null) {
            dto.setRouteName(securityPatrolRouteService.selectById(dto.getRouteId()).getName());
        }
        SecurityPatrolPlanEntity entity = CopyUtils.copyObj(dto, SecurityPatrolPlanEntity.class);
        entity.setId(Snowflake.nextId());
        securityPatrolPlanDao.insert(entity);
        return entity.getId();
    }

    @Override
    public void updateById(SecurityPatrolPlanDto dto) {
        if (dto.getRouteId() != null) {
            dto.setRouteName(securityPatrolRouteService.selectById(dto.getRouteId()).getName());
        }
        SecurityPatrolPlanEntity entity = CopyUtils.copyObj(dto, SecurityPatrolPlanEntity.class);
        securityPatrolPlanDao.updateById(entity);
        CacheKit.invalidRedisCache(SECURITY_PATROL_PLAN_CACHE_BY_ID_FUNC.apply(dto.getId()));
    }

    @Override
    public void updateAllColumnById(SecurityPatrolPlanDto dto) {
        SecurityPatrolPlanEntity entity = CopyUtils.copyObj(dto, SecurityPatrolPlanEntity.class);


        MyBatisPlusUtils.updateAllColumnById(entity, securityPatrolPlanDao);

        CacheKit.invalidRedisCache(SECURITY_PATROL_PLAN_CACHE_BY_ID_FUNC.apply(dto.getId()));
    }

    @Override
    public void deleteById(Long id) {
        securityPatrolPlanDao.deleteById(id);

        CacheKit.invalidRedisCache(SECURITY_PATROL_PLAN_CACHE_BY_ID_FUNC.apply(id));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteBatch(Long[] ids) {
        for (Long id : ids) {
            deleteById(id);
        }
    }
}
